package com.example.myhelloworld.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UserManagementExercise extends ExerciseClass{

	Button saveChanges = new Button("Save Changes");
	
	public UserManagementExercise(String uName, String settings,
			final GreetingServiceAsync GSA) {
		super(uName, settings, GSA);
		// TODO Auto-generated constructor stub
		
		selfPanel = new VerticalPanel();
		produceResultButton = new Button("Exit");
		selfPanel.addStyleName("ExerciseBasic");
		
		gsa.getAllUsers(new AsyncCallback<String>(){

			public void onFailure(Throwable caught) {
			}

			public void onSuccess(final String result) {
				String[] users = result.split("-");
				final HorizontalPanel[] horizontallist= new HorizontalPanel[users.length];
				for (int i = 0; i<users.length;i++){
					String username = users[i].split(":")[0];
					String usertype = users[i].split(":")[1];
					HorizontalPanel rowi = new HorizontalPanel();
					HTML usernameHtml = new HTML(username);
					usernameHtml.setWidth("100px");
					rowi.add(usernameHtml);
					String currentGroup = "user" + i;
					RadioButton student = new RadioButton(currentGroup, "Student");
					RadioButton teacher = new RadioButton(currentGroup, "Teacher");
					RadioButton admin   = new RadioButton(currentGroup, "Admin");
					if        (usertype.equals("student")){
						student.setValue(true);
					} else if (usertype.equals("teacher")){
						teacher.setValue(true);
					} else if (usertype.equals("admin")){
						admin.setValue(true);
					}
					
					rowi.add(student);
					rowi.add(teacher);
					rowi.add(admin);
					horizontallist[i] = rowi;
					
					selfPanel.add(rowi);	
					
				}
				HorizontalPanel buttonrow = new HorizontalPanel();
				buttonrow.add(saveChanges);
				saveChanges.addClickHandler(new ClickHandler(){

					public void onClick(ClickEvent event) {
						for (int i = 0; i<horizontallist.length;i++){
							//checks which items that have changed, updates those items..
							String uName =  ((HTML)horizontallist[i].getWidget(0)).getText();
							boolean check1 = ((RadioButton)horizontallist[i].getWidget(1)).getValue();
							boolean check2 = ((RadioButton)horizontallist[i].getWidget(2)).getValue();
							boolean check3 = ((RadioButton)horizontallist[i].getWidget(3)).getValue();

							String[] users = result.split("-");
							String username = users[i].split(":")[0];
							String usertype = users[i].split(":")[1];
							boolean haschanged = true; 
							// sets the flag as usnchanged if the current checkbox status is the same as previous.. 
							if ((check1 && (usertype.equals("student"))) || (check2 && (usertype.equals("teacher"))) || (check3 && (usertype.equals("admin")))){
								haschanged = false; 
							}
							if (haschanged) {
								String changeTo = "student";
								if ((check3 && !(usertype.equals("admin")))){
									changeTo = "admin";
								} else if ((check2 && !(usertype.equals("teacher")))){
									changeTo = "teacher";
								} if ((check1 && !(usertype.equals("student")))){
									changeTo = "student";
								}
								
								gsa.changeUser(username + ',' + changeTo, new AsyncCallback<String>(){

									public void onFailure(Throwable caught) {
									}
									public void onSuccess(String result) {
									}});
							}
						}
					}
				});
				buttonrow.add(produceResultButton);
				selfPanel.add(buttonrow);

			}});
		//changeUser
	}

	@Override
	public void draw() {}

	@Override
	public String pushresult() {
		return "";
	}

}
